<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <script>
      function insertSort(num1, num2) {
        let i = num1.length - 1//左指针
        let j = num2.length - 1 //右指针
        let s = i + j + 1  //合并后数组的指针
        console.log(i,j,s);
        while (i > 0 || j > 0) { //只要两边还有指针没有走完,就继续遍历
          if (i < 0) {//说明arr2还有元素没走完,右指针走一格
            num1[s] = num2[j]
            j--
          } else if (j < 0) {//说明arr1还有元素没走完
            num1[s] = num1[i]
            i--//左指针往前走一个
          } else if (num1[i] >= num2[j]) {//两边都没走完,判断大小,把大的填进去
            num1[s] = num1[i]
            i--//左指针汪前走一个
          } else {
            num1[s] = num2[j]
            j--//左指针往前走一个
          }
          s--
        }
        return num1
      }
      let num1 = [1, 2]
      let num2 = [2, 3]
      const res = insertSort(num1, num2)
      console.log(res)
    </script>
  </body>
</html>
